package com.dw.widget.slideshow.launcher;

import com.dw.widget.slideshow.db.helper.ScanFolderHelper;
import com.dw.widget.slideshow.db.object.ScanFolderData;
import com.dw.widget.slideshow.utils.AppConfig;
import com.dw.widget.slideshow.utils.ApplicationEx;
import com.dw.widget.slideshow.utils.log.Log;

import android.appwidget.AppWidgetHost;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;

public class WidgetProvider extends AppWidgetProvider {

	private final String TAG = "WidgetProvider";
	private ApplicationEx _APP = ApplicationEx.INSTANCE();
	
	@Override
	public void onEnabled(Context context) {

		Log.d(context, TAG, "WidgetProvider onEnabled");
		
		super.onEnabled(context);
		
    	AppConfig appConfig = new AppConfig(context, _APP.CONFIG_FILE_NAME);
    	appConfig.putBoolean(_APP.CONFIGURATION_DONE, false);
    	
	}

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		
		Log.d(context, TAG, "WidgetProvider onDeleted");
		
		Intent i = new Intent(context, SlideWidgetService.class);
		i.putExtra(_APP.INTENT_MSG_SERVICECOMMAND, _APP.SERVICE_CMD_STOPWIDGET);
		context.startService(i);
		
		for(int id : appWidgetIds)
		{
			deleteAppWidgetId(context, id);
		}
		
		super.onDeleted(context, appWidgetIds);
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		
		Log.d(context, TAG, "WidgetProvider onUpdate");
		
		AppConfig appConfig = new AppConfig(context, _APP.CONFIG_FILE_NAME);
        boolean configDone = appConfig.getBoolean(_APP.CONFIGURATION_DONE, false);
        if(configDone == false)
        {
        	Log.d(context, TAG, "Widget is configuring!");
        	return;
        }
		
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		
		Intent intent = new Intent(context, SlideWidgetService.class);
		intent.putExtra(_APP.INTENT_MSG_APPWIDGETIDS, appWidgetIds);
		intent.putExtra(_APP.INTENT_MSG_SERVICECOMMAND, _APP.SERVICE_CMD_STARTWIDGET);
		context.startService(intent);
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {

		super.onReceive(context, intent);
		int cmd = intent.getIntExtra(_APP.BROADCAST_INTENT_CMD, 0);
		String action = intent.getAction();
		
		Log.d(context, TAG, String.format("onReceive action=%1$s", action));
		
		if(cmd == _APP.BROADCAST_INTENT_TIMER)    // 切换图片定时器事件
		{
			Intent i = new Intent(context, SlideWidgetService.class);
			i.putExtra(_APP.INTENT_MSG_SERVICECOMMAND, _APP.SERVICE_CMD_SWITCHCLIP);
			context.startService(i);
			
			return;
		}
		
		if(action == Intent.ACTION_MEDIA_MOUNTED)    // SD卡挂载
		{
			// 启动切换
			Intent i = new Intent(context, SlideWidgetService.class);
			i.putExtra(_APP.INTENT_MSG_SERVICECOMMAND, _APP.SERVICE_CMD_STARTWIDGET);
			context.startService(i);
		}
		else if(action == Intent.ACTION_MEDIA_UNMOUNTED)   // SD卡卸载
		{
			// 暂停Widget切换
			Intent i = new Intent(context, SlideWidgetService.class);
			i.putExtra(_APP.INTENT_MSG_SERVICECOMMAND, _APP.SERVICE_CMD_PAUSEWIDGET);
			context.startService(i);
		}
	}

	public void deleteAppWidgetId(Context context,int appWidgetId){
		AppWidgetHost ah = new AppWidgetHost(context, 0);
        ah.deleteAppWidgetId(appWidgetId);
    }
}
